package pl.kozulowski.components {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class CheckBox extends MovieClip {
		
		private var _selected:Boolean = false;
		private var _data:Object;
		
		public function CheckBox() {
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init($e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			this.buttonMode = true;
			this.addEventListener(MouseEvent.CLICK, onClicked);
		}
		
		private function onClicked($e:MouseEvent):void {
			if(!_selected) {
				this.gotoAndStop(2);
				dispatchEvent(new Event(Event.SELECT));
				_selected = true;
				this.removeEventListener(MouseEvent.CLICK, onClicked);
			}
		}
		
		public function deselect():void {
			this.gotoAndStop(1);
			_selected = false;
			this.addEventListener(MouseEvent.CLICK, onClicked);
		}
		
		public function select():void {
			this.gotoAndStop(2);;
			_selected = true;
			this.removeEventListener(MouseEvent.CLICK, onClicked);
		}
		
		public function set data($d:Object):void {
			_data = $d;
		}
		
		public function get data():Object {
			return _data;
		}
		
		public function get selected():Boolean {
			return _selected
		}
	}
}